<?php
/**
 * Created by JetBrains PhpStorm.
 * User: h2a!
 * Date: 23/11/2012
 * Time: 10:56
 * To change this template use File | Settings | File Templates.
 */

class Manage_Catalog_Controller extends CI_Controller {
    function get_all() {
        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            $this->load->model('admin/manage_catalog_model');
            $data['query'] = $this->manage_catalog_model->get_all_catalogs();
            $this->load->view('admin/catalog_view', $data);
        }
    }

    function insert_catalog() {
        //load view and model

        $this->load-> model('admin/manage_catalog_model');

        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            $this->load->view('admin/catalog_insertion');
            //create values array to insert to database
            $category_id  = $this->input->post('maca');
            $category_name  = $this->input->post('tenca');
            $category_summary  = $this->input->post('summary');
            $data = array(
                'category_id' => $category_id,
                'category_name' => $category_name,
                'category_summary' => $category_summary
            );

            //if user clicks submit button, call to manage_catalog_model model to insert to database
            if($this->input->post('submit')) {
                $this->manage_catalog_model->insert_category($data);
            }
        }
    }

    public function edit_catalog($id = 0) {
        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            $this->load-> model('admin/manage_catalog_model');
            $data['query'] = $this->manage_catalog_model->get_catalog($id);
            $this->load->view('admin/catalog_edit', $data);

            $catalog_name = $this->input->post('tenca');
            $catalog_summary = $this->input->post('summary');
            $data2 = array(
                'category_name' => $catalog_name,
                'category_summary' => $catalog_summary,
            );

            if($this->input->post('edit')) {
                $this->manage_catalog_model->edit_category($data2, $id);
            }
        }
    }

    public function delete_catalog($id = 0) {
        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            $this->load->model('admin/manage_catalog_model');
            $this->manage_catalog_model->delete_category($id);
        }
    }

}

?>